﻿using EnvDTE;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.Shell;
using System;

namespace SPVSX.Project.Extensions.Wizards
{
    /// <summary>
    /// The SharePointServices object originally from the MS code
    /// </summary>
    public class SharePointServices
    {
        #region Fields

        /// <summary>
        /// Field to back the Project property
        /// </summary>
        private ISharePointProject _project;

        /// <summary>
        /// Field to back the Project Service property
        /// </summary>
        private ISharePointProjectService _projectService;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the ISharePointProject
        /// </summary>
        public ISharePointProject Project
        {
            get
            {
                return _project;
            }
        }

        /// <summary>
        /// Gets the ISharePointProjectService
        /// </summary>
        public ISharePointProjectService ProjectService
        {
            get
            {
                return _projectService;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Initialise a new SharePointServices object
        /// </summary>
        /// <param name="project">The Project object</param>
        public SharePointServices(EnvDTE.Project project)
        {
            _projectService = GetProjectService(project.DTE);
            _project = _projectService.Convert<EnvDTE.Project, ISharePointProject>(project);
        }

        /// <summary>
        /// Get the project service
        /// </summary>
        /// <param name="designTimeEnvironment">The design time environment</param>
        /// <returns>The ISharePointProjectService object</returns>
        internal static ISharePointProjectService GetProjectService(DTE designTimeEnvironment)
        {
            Microsoft.VisualStudio.OLE.Interop.IServiceProvider sp = designTimeEnvironment as Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
            return (new ServiceProvider(sp).GetService(typeof(ISharePointProjectService)) as ISharePointProjectService);
        }

        #endregion
    }
}
